import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class KalOnline_GB(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(KalOnline_GB,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.version = 0
        
    def parse_material_header(self, numMats):
        
        for i in range(numMats):
            
            texNameOffset = self.inFile.read_long()
            texMapOption = self.inFile.read_short()
            texNameLen = self.inFile.read_long()
            texOverlayStrOffset = self.inFile.read_long()
            matOffset = self.inFile.read_long()
        
    def parse_mesh(self, vertType, numVerts, numFaces, numBones):
        
        for i in range(numBones):
            self.inFile.read_byte()
            
        for i in range(numVerts):
            self.inFile.seek((vertType % 4) * 4 + 32)
            
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
        
    def parse_bones(self, numBones):
        
        for i in range(numBones):
            transform = self.inFile.read_float(16)
            boneID = self.inFile.read_byte()
        
    def parse_mesh_header(self, numMesh):
        
        for i in range(numMesh):
            nameOffset, matNum = self.inFile.read_long(2)
            vertType, primitiveType = self.inFile.read_byte(2)
            numVerts = self.inFile.read_short(sign=0)
            numFaces = (self.inFile.read_short(sign=0) * 2) / 3
            numBones = self.inFile.read_byte()
            print numVerts, numFaces
            self.parse_mesh(vertType, numVerts, numFaces, numBones)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        self.version, numBones, boneID, numMats = self.inFile.read_byte(4)
        
        print self.version
        if self.version >= 0x0B:
            checkSum = self.inFile.read_long()
        if self.version == 0x0C:
            name = self.inFile.read_string(64)
        nameLen = self.inFile.read_long()
        if self.version == 0x08:
            totalVerts = self.inFile.read_short(6)
        else:
            totalVerts = self.inFile.read_short(12)
        totalFaces = self.inFile.read_short()
        totalBones = self.inFile.read_short()
        totalKeyFrames = self.inFile.read_short()
        unk0 = self.inFile.read_short() 
        matDescLen = self.inFile.read_short()
        unk1 = self.inFile.read_short()
        if self.version >= 0x09:
            collisionSize = self.inFile.read_long()
            
        numTransform = self.inFile.read_short()
        numAnim = self.inFile.read_byte()
        if self.version in [0x09, 0x0A, 0x0B]:
            unk3 = self.inFile.read_byte()
        numMesh = self.inFile.read_short()        
        unk2 = self.inFile.read_short()
        if self.version in [0x09, 0x0A, 0x0B, 0x0C]:
            if self.version >= 0x0B:
                boundBoxMin = self.inFile.read_float(3)
                boundBoxMax = self.inFile.read_float(3)
            boundSphereCenter = self.inFile.read_long(3)
            boundSphereRadius = self.inFile.read_float()
            
        
        print self.inFile.tell()
        self.parse_material_header(numMats)
        self.parse_mesh_header(numMesh)
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = KalOnline_GB(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "GB", "Kal Online GB"

if __name__ == '__main__':
    
    file1 = "dropitem07.gb"
    file2 = "dropitem01.gb"
    obj = read_file(file2)